package com.whfc.fuum.xxl.job;

import com.alibaba.fastjson.JSON;
import com.whfc.XxlJobConfig;
import com.whfc.common.enums.AppType;
import com.whfc.common.weixin.bean.WxAccessToken;
import com.whfc.common.weixin.mp.TokenApi;
import com.whfc.fuum.dao.WxConfigMapper;
import com.whfc.fuum.dto.WxConfigDTO;
import com.whfc.fuum.redis.WxTokenRedisDao;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;

/**
 * @Description: 小程序定时刷新任务
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/8/6 10:36
 */
@Component
@ConditionalOnBean(XxlJobConfig.class)
public class WxMpAccessTokenJob {


    @Autowired
    private WxConfigMapper wxConfigMapper;

    @Autowired
    private WxTokenRedisDao wxTokenRedisDao;


    @PostConstruct
    public void init() {
        refreshAccessToken();
    }

    @XxlJob("refreshAccessToken")
    public void refreshAccessToken() {

        XxlJobHelper.log("获取军机处小程序accessToken...");
        this.refreshAccessToken(AppType.JJC);

        XxlJobHelper.log("获取工机云小程序accessToken...");
        this.refreshAccessToken(AppType.GJY);

        XxlJobHelper.log("获取物资管理小程序accessToken...");
        this.refreshAccessToken(AppType.FMAM);

        XxlJobHelper.log("获取环焊缝小程序accessToken...");
        this.refreshAccessToken(AppType.HHF);

        XxlJobHelper.log("获取视频监控小程序accessToken...");
        this.refreshAccessToken(AppType.FVS);

        XxlJobHelper.log("获取考勤设备小程序accessToken...");
        this.refreshAccessToken(AppType.EQUIP);

        XxlJobHelper.log("获取人员管理小程序accessToken...");
        this.refreshAccessToken(AppType.EMP);

        XxlJobHelper.log("获取人融E建小程序accessToken...");
        this.refreshAccessToken(AppType.REJ);

        //logger.info("获取CMS网站应用accessToken...");
        //this.refreshAccessToken(AppType.CMS);
    }

    /**
     * 刷新微信accessToken
     *
     * @param appType
     */
    private void refreshAccessToken(AppType appType) {
        try {
            XxlJobHelper.log("刷新accessToken,appType:{}", appType);
            WxConfigDTO wxConfigDTO = wxConfigMapper.selectConfigByAppType(appType.name());
            if (wxConfigDTO != null) {
                String appId = wxConfigDTO.getAppId();
                String secret = wxConfigDTO.getAppSecret();
                String result = TokenApi.getAccessToken(appId, secret);
                WxAccessToken token = JSON.parseObject(result, WxAccessToken.class);
                if (token != null && !StringUtils.isEmpty(token.getAccessToken())) {
                    wxTokenRedisDao.setAccessToken(appType.name(), token.getAccessToken(), token.getExpiresIn());
                }
            }
        } catch (Exception e) {
            XxlJobHelper.handleFail("刷新token失败 error" + e.getMessage());
        }
    }
}
